/*
 * Created by SharpDevelop.
 * Author: Joey
 * Date: 24/09/2007
 * Time: 22:43
 * 
 * Source is copyright Joey Sabey 2007
 */

using System;
using System.IO;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

using Creatures;

namespace MonoTheism
{
	/// <summary>
	/// Description of MainForm.
	/// </summary>
	public partial class MainForm : Form
	{
		public MainForm()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			

		}
		
		void TsmiFILEClick(object sender, EventArgs e)
		{
			FILE_Properties fpDialogue = new FILE_Properties();
			fpDialogue.ShowDialog();
			if(fpDialogue.AcceptChanges)
			{
				TreeNode tnBuffer = new TreeNode(fpDialogue.EmbedName);
				FILE_Block fbBuffer = new FILE_Block(fpDialogue.Source,
				                                     fpDialogue.EmbedName);
				tnBuffer.Tag = fbBuffer;
				treeBlocks.Nodes.Add(tnBuffer);
				//Program.pyFile.Add(fpDialogue.Source, fpDialogue.EmbedName);
			}
		}
		
		void StbRemoveClick(object sender, EventArgs e)
		{
			try
			{
				treeBlocks.SelectedNode.Remove();
			}
			catch(NullReferenceException)
			{
				WarningDialogue warning = new WarningDialogue("Nothing was selected.");
				warning.ShowDialog();
			}
		}
		
		void AboutToolStripMenuItemClick(object sender, EventArgs e)
		{
			AboutBox about = new AboutBox();
			about.ShowDialog();
		}
		
		void TsbEditClick(object sender, EventArgs e)
		{
			try
			{
				TreeNode tnBuffer = treeBlocks.SelectedNode;
				Block tag = (Block)tnBuffer.Tag;
				if(tag.Type() == "FILE")
				{
					FILE_Block fbBuffer = (FILE_Block)tag;
					FILE_Properties properties = new
									FILE_Properties(fbBuffer.Source,
													fbBuffer.EmbedName);
					properties.ShowDialog();
					if(properties.AcceptChanges == true)
					{
						fbBuffer = new FILE_Block(properties.Source,
						                          properties.EmbedName);
						treeBlocks.SelectedNode.Tag = fbBuffer;
						treeBlocks.SelectedNode.Text = properties.EmbedName;
					}
				}
				else if(tag.Type() == "AGNT")
				{
					AGNT_Block abBuffer = (AGNT_Block)tag;
					AGNT_Properties properties = new AGNT_Properties(abBuffer);
					properties.ShowDialog();
					if(properties.AcceptChanges)
					{
						abBuffer = properties.Agent;
						treeBlocks.SelectedNode.Tag = abBuffer;
						treeBlocks.SelectedNode.Text = properties.AgentName;
					}
				}
			}
			catch(NullReferenceException)
			{
				WarningDialogue warning = new WarningDialogue("Nothing was selected.");
				warning.ShowDialog();
			}
		}
		
		void TsmiAGNTClick(object sender, EventArgs e)
		{
			AGNT_Properties apDialogue = new AGNT_Properties();
			apDialogue.ShowDialog();
			
			if(apDialogue.AcceptChanges)
			{
				TreeNode tnBuffer = new TreeNode(apDialogue.AgentName);
				tnBuffer.Tag = apDialogue.Agent;
				treeBlocks.Nodes.Add(tnBuffer);
			}
		}
		
		void ExportToolStripMenuItemClick(object sender, EventArgs e)
		{
			Output.Export(treeBlocks.Nodes);
		}
		
		void SaveToolStripMenuItemClick(object sender, EventArgs e)
		{
			if(Global.FilePath != null)
			{
				Output.Save(treeBlocks.Nodes, Global.FilePath);
			}
			else
			{
				SaveFileDialog save = new SaveFileDialog();
				if(save.ShowDialog() == DialogResult.OK)
				{
					Output.Save(treeBlocks.Nodes, save.FileName);
					Global.FilePath = save.FileName;
				}
			}
		}
		
		void OptionsToolStripMenuItemClick(object sender, EventArgs e)
		{
			OptionsDialogue odDialogue = new OptionsDialogue();
			odDialogue.ShowDialog();
		}
		
		void TsbOptionsClick(object sender, EventArgs e)
		{
			OptionsDialogue odDialogue = new OptionsDialogue();
			odDialogue.ShowDialog();
		}
		
		void BuildToolStripMenuItemClick(object sender, EventArgs e)
		{
			FileName_Properties fnpDialogue = new FileName_Properties();
			fnpDialogue.ShowDialog();
			if(fnpDialogue.AcceptChanges)
			{
				foreach(string s in Options.AutoBuild)
				{
					Output.Export(treeBlocks.Nodes,
				    	          (s +
					               Path.DirectorySeparatorChar +
					               fnpDialogue.FileName));
				}
			}
		}
		
		void ExitToolStripMenuItemClick(object sender, EventArgs e)
		{
			QueryDialogue qd = new QueryDialogue("Really Quit?", 
			                                     "All unsaved changes will be lost. Are you certain you wish to quit?");
			qd.ShowDialog();
			if(qd.Yes)
				Close();
		}
		
		void SaveAsToolStripMenuItemClick(object sender, EventArgs e)
		{
			SaveFileDialog save = new SaveFileDialog();
			if(save.ShowDialog() == DialogResult.OK)
			{
				Output.Save(treeBlocks.Nodes, save.FileName);
				Global.FilePath = save.FileName;
			}
		}
		
		void TsbInfoClick(object sender, EventArgs e)
		{
			treeBlocks = BlockController.RefreshNodes(treeBlocks);
		}
	}
}
